package com.enjava;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

@ManagedBean(name="alumnoBean")
@RequestScoped
public class AlumnosBean {

	Alumno alumno = new Alumno();
	List<Curso> cursos;
	@ManagedProperty(value = "#{emf}")
	EMF emf;

	public Alumno getAlumno() {
		return alumno;
	}

	public void setAlumno(Alumno alumno) {
		this.alumno = alumno;
	}

	public EMF getEmf() {
		return emf;
	}

	public void setEmf(EMF emf) {
		this.emf = emf;
	}
	
	
	public List<Curso> getCursos() {
		return cursos;
	}

	public void setCursos(List<Curso> cursos) {
		this.cursos = cursos;
	}

	public String nuevoAlumno(){
		
		return "formularioNuevoAlumno";
		
	}
	
	
	public String guardarAlumno(){
	
		System.out.println("guardando alumno");
		System.out.println(alumno.getNombre());
		EntityManager em = emf.getEM();
		
		EntityTransaction transaccion = em.getTransaction();
		transaccion.begin();
		em.persist(alumno);
		transaccion.commit();
		
		return "home";
		
	}
	
	
	@PostConstruct
	public void preparaBean(){
		
		EntityManager em = emf.getEM();
		Query sql = em.createQuery("SELECT Curso FROM Curso Curso");
		cursos = sql.getResultList();
		
	}
	
	
}
